/***************************************************************************************************
 The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); 
 you may not use this file except in compliance with the License. You may obtain a copy of the    
 License at http://www.mozilla.org/MPL/                                                           
                                                                                                  
 Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF   
 ANY KIND, either express or implied. See the License for the specific language governing rights  
 and limitations under the License.                                                               
                                                                                                  
 The Original Code is default.aspx.cs                                                             
                                                                                                  
 The Initial Developer of the Original Code is Fabrizio Vita (fabrizio.vita@itacasoft.com)        
***************************************************************************************************/
using System;
using TkSilverlight.DAO;
using System.IO;
using TkSilverlight.DAL;
using TkSilverlight.DAO.Entities;

namespace TKIsapiClient.Web.POI
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int affected = 0;
            string xml = "";
            bool exists = false;
            PoiDAO dao = null;

            try
            {
                try
                {
                    string action = Utils.GetParamAsString(Request, "action", out exists);
                    action = action.ToLower();

                    string userid = Utils.GetParamAsString(Request, "userid", out exists);
                    if (!exists)
                        throw new Exception("parametro userid obbligatorio");

                    string id = Utils.GetParamAsString(Request, "id", out exists);
                    if ((!exists) && (action == "delete"))
                    {
                        throw new Exception("parametro id obbligatorio");
                    }

                    string category = Utils.GetParamAsString(Request, "category", out exists);
                    if ((!exists) && (action == "list"))
                        throw new Exception("parametro category obbligatorio");


                    if (action == "save")
                    {
                        dao = new PoiDAO();
                        dao.Open();

                        xml = new StreamReader(Request.InputStream).ReadToEnd();
                        TkSilverlight.DAO.Entities.POI poi = ObjectXMLSerializer<TkSilverlight.DAO.Entities.POI>.DeSerializeObject(xml);

                        affected = dao.Save(Sql.ToGuid(userid), poi);
                    }
                    else if (action == "delete")
                    {
                        dao = new PoiDAO();
                        dao.Open();

                        affected = dao.Delete(id);
                    }
                    else if (action == "list")
                    {
                        dao = new PoiDAO();
                        dao.Open();

                        xml = dao.ListAsXML(userid, category);
                        Response.Clear();
                        Response.StatusCode = 200;
                        Response.ContentType = "text/xml";

                        Response.Output.Write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                        Response.Output.Write(xml);
                        Response.Flush();
                        return;
                    }

                    Response.Clear();
                    Response.StatusCode = 200;
                    Response.ContentType = "text/xml";

                    xml = Utils.GetXMLResult(affected);
                    Response.Output.Write(xml);
                    Response.Flush();
                }
                catch (Exception ex)
                {
                    Utils.SendExceptionToClient(Response, ex);
                }
            }
            finally
            {
                Response.End();

                if (dao != null)
                    dao.Dispose();
            }


        }
    }
}
